package com.ruoyi.common.utils.poi.word.render;

import com.aspose.words.IMailMergeDataSource;
import com.aspose.words.ref.Ref;
import java.util.Map;

/**
 * 因为Aspose.Words for Java不支持HashMap的数据格式，所以这个类是为了实现对HashMap的支持
 */
public class MapMailMergeDataSource implements IMailMergeDataSource {
    private Map<String, Object> data;
    private int index;
    private String tableName = null;
    private boolean isMove = false;

    /**
     * @param data      单个数据集
     * @param tableName 与模板中的Name对应
     */
    public MapMailMergeDataSource(Map<String, Object> data, String tableName) {
        this.data = data;
        this.tableName = tableName;
        index = -1;
    }

    @Override
    public IMailMergeDataSource getChildDataSource(String arg0)
            throws Exception {
        return null;
    }

    @Override
    public String getTableName() throws Exception {
        return this.tableName;
    }

    @Override
    public boolean moveNext() throws Exception {
        if (isMove) {
            return false;
        }
        isMove = true;
        return true;
    }

    /**
     * 实现接口
     * 获取当前index指向数据行的数据
     * 将数据存入args数组中即可
     *
     * @return ***返回false则不绑定数据***
     */
    @Override
    public boolean getValue(String fieldName, Ref<Object> fieldValue) throws Exception {
        fieldName = fieldName.replace(tableName + ".", "");
        if (data != null && data.containsKey(fieldName)) {
            fieldValue.set(data.get(fieldName));
            return true;
        }
        return false;
    }
}